package com.sfzd5.aiclassroom;

import com.google.gson.Gson;
import com.sfzd5.aiclassroom.modle.ExamQuestionsInfo;
import com.sfzd5.aiclassroom.modle.Question;
import com.sfzd5.exam.helplibrary.exam.Answer;
import com.sfzd5.exam.helplibrary.exam.answersheet.Area;
import org.apache.commons.io.FileUtils;
import org.json.JSONObject;

import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;

public class JsonHelper {
    File jsonFile;
    JSONObject root;

    HashMap<Integer, String> SubjectMap = new HashMap<Integer, String>(){
        {
            put(2, "语文");
            put(3,"数学");
            put(4,"英语");
            put(6,"物理");
            put(7,"化学");
            put(8,"历史");
            put(9,"政治思品");
            put(10,"地理");
            put(11,"生物");
        }
    };
    HashMap<Integer, String> QuestionTypeMap = new HashMap<Integer, String>(){
        {
            put(1,"单选题");
            put(2,"多选题");
            put(3,"判断题");
            put(4,"填空题");
            put(5,"计算题");
            put(6,"解答题");
            put(9,"问答题");
            put(25,"作图题");
            put(26,"实验探究题");
            put(27,"材料分析题");
            put(28,"综合题");
            put(30,"连线题");
            put(38,"双选题");
        }
    };

    public JsonHelper(){

    }

    public JsonHelper(File jsonFile){
        this.jsonFile = jsonFile;
        try {
            String json = FileUtils.readFileToString(jsonFile, "utf-8");
            root = new JSONObject(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(getId());
    }

    public JsonHelper(String json){
        root = new JSONObject(json);
    }

    public int getId(){
        return root.getInt("pid");
    }

    public String getSubject(){
        return SubjectMap.get(root.getInt("chid"));
    }

    public String getTitle(){
        return root.getString("title");
    }

    public int getQuestionNum(){
        return root.getInt("question_num");
    }

    class SortContent implements Comparator<JSONObject> {
        /*
put(1,"单选题");
put(2,"多选题");
put(3,"判断题");
put(4,"填空题");
put(5,"计算题");
put(6,"解答题");
put(9,"问答题");
put(25,"作图题");
put(26,"实验探究题");
put(27,"材料分析题");
put(28,"综合题");
put(30,"连线题");
put(38,"双选题");
    */
        int getTypeNum(JSONObject o) {
            String t = o.getString("head_title");
            if (t.contains("选")){
                return 1;
            } else if(t.contains("填空")){
                return 2;
            } else {
                return 3;
            }
        }
        @Override
        public int compare(JSONObject o1, JSONObject o2) {
            return Integer.compare(getTypeNum(o1), getTypeNum(o2));
        }
    }
    public ArrayList<JSONObject> getContent(){
        ArrayList<JSONObject> cs = new ArrayList<>();
        for(Object o : root.getJSONArray("content")){
            cs.add((JSONObject) o);
        }
        cs.sort(new SortContent());
        return cs;
    }

    private void setQuestionTextChild(JSONObject j, Question qq){
        StringBuilder t = new StringBuilder();
        t.append("<div class='question'>\n");

        t.append("<div class='question_text'>" + j.getString("question_text") + "</div>\n");

        if(j.has("options") && !j.isNull("options")){
            JSONObject op = j.getJSONObject("options");
            t.append("<div class='options'>\n");
            for(String k : op.keySet()){
                t.append("<div class='option_item'>" + k + ". " + op.get(k) + "</div>\n");
            }
            t.append("</div>\n");
        }

        if(j.has("list") && !j.isNull("list")){
            qq.child = new ArrayList<>();
            t.append("<div class='child_question'>\n");
            int i = 0;
            for(Object ec : j.getJSONArray("list")){
                JSONObject sq = (JSONObject) ec;

                qq.child.add(toChildQuestion(sq));

                i++;
                t.append("<div class='child_item'>" + i + ". " + sq.getString("question_text") + "</div>\n");
            }
            t.append("</div>\n");
        }

        t.append("</div>\n");

        String s = t.toString();
        qq.question_text = s.replaceAll("\\{#blank#\\}\\d*\\{#/blank#\\}", "________________");
    }

    private Area.AreaType getAnswerType(String t){
        if(t.contains("选")){
            return Area.AreaType.Choice;
        } else  if(t.contains("填空")){
            return Area.AreaType.Blank;
        } else{
            return Area.AreaType.Expound;
        }
    }

    private Answer getAnswer(String t, String a){
        Answer answer = new Answer();
        answer.content = a;
        answer.type = getAnswerType(t);
        return answer;
    }

    public Question toQuestion(JSONObject q){
        Question qq = new Question();
        qq.id = q.getLong("question_id");

        qq.questiontype = q.getString("channel_type_name");
        qq.subject = SubjectMap.get(q.getInt("chid"));

        setQuestionTextChild(q, qq);

        //出现错误 JSONObject["answer_text"] is not a string.
        if(q.isNull("answer_text"))
            qq.answer = "";
        else
            qq.answer = new Gson().toJson(getAnswer(qq.questiontype, q.getString("answer_text")));

        qq.difficult = q.getString("difficult_name");
        return qq;
    }


    private Question toChildQuestion(JSONObject q){
        Question qq = new Question();
        qq.questiontype = QuestionTypeMap.get(q.getInt("question_type"));
        setQuestionTextChild(q, qq);
        if(q.has("answer_text") && !q.isNull("answer_text"))
            qq.answer = new Gson().toJson(getAnswer(qq.questiontype, q.getString("answer_text")));
        else
            qq.answer = "";
        return qq;
    }

    public HashMap<String, ArrayList<Question>> getQuestionContent() {
        HashMap<String, ArrayList<Question>> content = new HashMap<>();
        for (JSONObject cc : getContent()) {
            String title = cc.getString("head_title");
            ArrayList<Question> qs = new ArrayList<>();
            for (Object q : cc.getJSONArray("questions")) {
                JSONObject qq = (JSONObject) q;
                Question qt = toQuestion(qq.getJSONObject("question"));

                qt.related = new ArrayList<>();
                if(cc.has("related_questions"))
                    for (Object rq : qq.getJSONArray("related_questions"))
                        qt.related.add(toQuestion((JSONObject) rq));

                qs.add(qt);
            }
            content.put(title, qs);
        }
        return content;
    }

    public ExamQuestionsInfo toExamQuestionsInfo(){
        ExamQuestionsInfo info = new ExamQuestionsInfo();
        info.id = getId();
        info.examName = getTitle();
        info.subject = getSubject();
        info.content = getQuestionContent();
        return info;
    }
}
